document.addEventListener("DOMContentLoaded", () => {
    const loginButton = document.querySelector(".login-button");
    const avatarMenu = document.querySelector(".avatar-menu");
    const usernameDisplay = document.querySelector(".username-display");

    // 检查是否登录
    const user = JSON.parse(localStorage.getItem("user"));
    if (user) {
        if (loginButton) loginButton.style.display = "none";
        if (avatarMenu) avatarMenu.style.display = "block";
        if (usernameDisplay) usernameDisplay.textContent = user.username;
    } else {
        if (loginButton) loginButton.style.display = "block";
        if (avatarMenu) avatarMenu.style.display = "none";
    }

    // 登出逻辑
    const logoutBtn = document.querySelector("#logout-btn");
    if (logoutBtn) {
        logoutBtn.addEventListener("click", () => {
            localStorage.removeItem("user");
            alert("已退出登录");
            window.location.href = "index.html";
        });
    }
});